/*************************************************************************************
*Copyright (c) 2011, Intel Mobile Communications GmbH. 
*All rights reserved.
*
*Redistribution and use in source and binary forms, with or without
*modification, are permitted provided that the following conditions are met:
*    * Redistributions of source code must retain the above copyright
*      notice, this list of conditions and the following disclaimer.
*    * Redistributions in binary form must reproduce the above copyright
*      notice, this list of conditions and the following disclaimer in the
*      documentation and/or other materials provided with the distribution.
*    * Neither the name of the <organization> nor the
*      names of its contributors may be used to endorse or promote products
*      derived from this software without specific prior written permission.
*
*THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
*ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
*WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
*DISCLAIMED. IN NO EVENT SHALL INTEL MOBILE COMMUNICATIONS GMBH. BE LIABLE FOR ANY
*DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
*(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
*LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
*ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
*(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
*SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*************************************************************************************/



#include "cef_flow.h"
using namespace::std;
cef_burst_trans::cef_burst_trans()
{
	size=0;
	period=0;
	duration=0;
	offset=0;
}
cef_burst_trans::cef_burst_trans(unsigned s, unsigned long p, unsigned long o, unsigned long d):size(s),period(p),offset(o),duration(d)
{
}
cef_burst_trans::~cef_burst_trans()
{
}
//Setters
void cef_burst_trans::setSize(unsigned s)
{
	size=s;	
}
void cef_burst_trans::setPeriod(unsigned long p)
{
	period=p;
}
void cef_burst_trans::setOffset(unsigned long o)
{
	offset=o;
}
void cef_burst_trans::setDuration(unsigned long d)
{
	duration=d;
}
//Getters
unsigned cef_burst_trans::getSize()
{
	return size;
}
unsigned long cef_burst_trans::getPeriod()
{
	return period;
}

unsigned long cef_burst_trans::getOffset()
{
	return offset;

}
unsigned long cef_burst_trans::getDuration()
{
	return duration;
}

#ifdef _DEBUG
void cef_burst_trans::print()
{
	cout<<"CEF Burst Transfer\nBEGIN\n";
	cout<<"Size: "<<size<<" Period: "<<period<<" Offset: "<<offset<<" Duration: "<<duration<<endl;
	cout<<"END\n";
}
#endif

cef_burst::cef_burst()
{
}

cef_burst::cef_burst(unsigned l, double p):length(l),percent(p)
{
}

cef_burst::~cef_burst()
{
}

void cef_burst::setLength(unsigned l)
{
	length=l;		
}
void cef_burst::setPercent(double p)
{
	percent=p;
}
double cef_burst::getPercent()
{
	return percent;
}
unsigned cef_burst::getLength()
{
	return length;	
}
#ifdef _DEBUG
void cef_burst::print()
{
	cout<<"CEF Burstiness\nBEGIN\n";
	cout<<"Percent: "<<percent<<" Length: "<<length<<endl;
	cout<<"END\n";
}
#endif

cef_flow::cef_flow()
{
	id=0;
	type="";
	max_outstanding_transactions=0;
	peak_bandwidth=0;
	sustained_bandwidth=0;
	lat_zl=-1;
	lat_rt=-1;
	gid=-1;
	priority_class=0;
	prioritized=false;	
	burstiness=NULL;
	burst_transfers=NULL;
}
cef_flow::cef_flow(unsigned i, string t, unsigned mot, double pb, double sb, double zl, double rt, int g, bool p, unsigned pc, vector<cef_burst_trans*>* bt, vector<cef_burst*>* bness)
{
	id=i;	
	type=t;
	max_outstanding_transactions=mot;
	peak_bandwidth=pb;
	sustained_bandwidth=sb;
	lat_zl=zl;
	lat_rt=rt;
	gid=g;
	prioritized=p;
	priority_class=pc;
	burstiness=bness;
	burst_transfers=bt;
}
cef_flow::~cef_flow()
{
	if(burstiness)
	{
		for(unsigned i=0;i<burstiness->size();i++)
		{
			delete (*burstiness)[i];
		}	
		delete burstiness;
	}
	if(burst_transfers)
	{
		for(unsigned i=0;i<burst_transfers->size();i++)
		{
			delete (*burst_transfers)[i];
		}	
		delete burst_transfers;
	}
}
//Setters
void cef_flow::setType(std::string t)
{
	type=t;
}

void cef_flow::setID(unsigned i)
{
	id=i;
}
void cef_flow::setMOT(unsigned mot)
{
	max_outstanding_transactions=mot;	
}
void cef_flow::setPeakBW(double pb)
{
	peak_bandwidth=pb;	
}
void cef_flow::setSustainedBW(double sb)
{
	sustained_bandwidth=sb;	
}
void cef_flow::setZeroLoadLatB(double zl)
{
	lat_zl=zl;
}
void cef_flow::setLatB(double rt)
{
	lat_rt=rt;
}
void cef_flow::setGID(int g)
{
	gid=g;	
}

void cef_flow::setPrioritized(bool p)
{
	prioritized=p;
}

void cef_flow::setPriority(unsigned p)
{
	priority_class=p;
}

void cef_flow::setBurstTrans(vector<cef_burst_trans*>* bt)
{
	burst_transfers=bt;
}
void cef_flow::setBurstiness(vector<cef_burst*>* bn)
{
	burstiness=bn;
}
void cef_flow::addBurstTrans(cef_burst_trans* bt)
{
	if(burst_transfers==NULL) burst_transfers=new vector<cef_burst_trans*>();
	burst_transfers->push_back(bt);
}
void cef_flow::addBurstiness(cef_burst* bn)
{
	if(burstiness==NULL) burstiness=new vector<cef_burst*>();
	burstiness->push_back(bn);
}
//Getters
std::string cef_flow::getType()
{
	return type;
}

unsigned cef_flow::getID()
{
	return id;
}
unsigned cef_flow::getMOT()
{
	return max_outstanding_transactions;
}
double cef_flow::getPeakBW()
{
	return peak_bandwidth;
}
double cef_flow::getSustainedBW()
{
	return sustained_bandwidth;
}
double cef_flow::getZeroLoadLatB()
{
	return lat_zl;
}
double cef_flow::getLatB()
{
	return lat_rt;
}
bool cef_flow::isPrioritized()
{
	return prioritized;
}

unsigned cef_flow::getPriority()
{
	return priority_class;
}
int cef_flow::getGID()
{
	return gid;
}
vector<cef_burst_trans*>* cef_flow::getBurstTrans()
{
	return burst_transfers;
}
vector<cef_burst*>* cef_flow::getBurstiness()
{
	return burstiness;
}
cef_burst_trans* cef_flow::getBurstTrans_i(unsigned i)
{
	if(burst_transfers) return (*burst_transfers)[i];
	return NULL;
}
cef_burst* cef_flow::getBurstiness_i(unsigned i)
{
	if(burstiness) return (*burstiness)[i];
	return NULL;
}
unsigned cef_flow::sizeBurstTrans()
{
	if(burst_transfers) return burst_transfers->size();
	return 0;
}
unsigned cef_flow::sizeBurstiness()
{
	if(burstiness) return burstiness->size();
	return 0;
}

#ifdef _DEBUG
void cef_flow::print()
{
	cout<<"CEF Flow\nBEGIN\n";
	cout<<"ID: "<<id<<" Type: "<<type<<" MOT: "<<max_outstanding_transactions<<" Peak Bandwidth: "<<peak_bandwidth<<" Sustained Bandwidth: "<<sustained_bandwidth<<" Latency 0Load Bound: "<<lat_zl<<" Latency Bound: "<<lat_rt<<" Priority Class: "<<priority_class<<" Prioritized: "<<prioritized<<" GID: "<<gid<<endl;
	if(burst_transfers)
	{
		for(unsigned i=0;i<burst_transfers->size();i++)
		{
			(*burst_transfers)[i]->print();
		}
	}
	if(burstiness)
	{
		for(unsigned i=0;i<burstiness->size();i++)
		{
			(*burstiness)[i]->print();
		}
	}
	cout<<"END\n";
}
#endif
